﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post.Service;
using WuDada.Core.SystemApplications.Domain;
using WuDada.Core.Member.Domain;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;

public partial class member_rent_edit : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private WebLogService m_WebLogService;
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private SessionHelper m_SessionHelper = new SessionHelper();

    //熱門案件 NodeId=8
    private int m_NodeId = 8;
    private int m_PostId;

    private string m_PicFileName
    {
        get { if (ViewState["picfilename"] == null) { ViewState["picfilename"] = string.Empty; } return ViewState["picfilename"].ToString(); }
        set { ViewState["picfilename"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_WebLogService = new WebLogService();
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_MemberFactory = new MemberFactory();
        m_MemberService = m_MemberFactory.GetMemberService();

        if (!IsPostBack)
        {
            m_PostId = QueryStringHelper.GetInteger("id", 0);
            hdnId.Value = m_PostId.ToString();
            LoadVOToUI();
        }
    }

    private void LoadVOToUI()
    {
        PostVO postVO = m_PostService.GetPostById(int.Parse(hdnId.Value));
        CheckPost(postVO);
        UIHelper.FillUI(pnlUI, postVO);
        m_PicFileName = postVO.PicFileName;
        ltlImg.Text = GetPic(m_PicFileName);
    }

    private void CheckPost(PostVO postVO)
    {
        if (postVO == null || !m_SessionHelper.Member.MemberId.ToString().Equals(postVO.CreateMember) || postVO.Flag == 1)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("無此資料修改權限!", "member_rent.aspx"), false);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        PostVO postVO = m_PostService.GetPostById(int.Parse(hdnId.Value));
        CheckPost(postVO);
        UIHelper.FillVO(pnlUI, postVO);
        if (string.IsNullOrEmpty(postVO.Area))
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請選擇縣市與鄉鎮市區"), false);
        }
        else if (!RegexHelper.CheckTwAddress(postVO.Address))
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("地址格式錯誤"), false);
        }
        postVO.PicFileName = m_PicFileName;
        postVO.CustomField3 = postVO.Title;
        postVO.UpdatedDate = DateTime.Now;
        m_PostService.UpdatePost(postVO);

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("更新成功!", "member_rent.aspx"), false);
    }

    private string GetPic(string fileName)
    {
        return "<a href='upload/" + fileName + "' target='_blank' >" + "<img src='upload/" + fileName + "' width='40' height='30' border='0'>" + "</a>";
    }

    protected void btnUpliad_Click(object sender, EventArgs e)
    {
        try
        {
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetFileName(hpf.FileName);
                    hpf.SaveAs(Server.MapPath("~\\") + "\\upload\\" + fileName);
                    ltlImg.Text = GetPic(fileName);
                    m_PicFileName = fileName;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請點選您要上傳的照片!"), false);
                    return;
                }
            }
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("檔案傳輸錯誤!"), false);
            return;
        }
    }
}